package com.sino.business.service;

/**
 * Copyright (C)          : Sinodigital Tech. Co., Ltd. 
 * Project                : 专利项目申请 JDK version
 * used                   : jdk1.5.0_08 
 * Description            : 提供基本信息业务逻辑接口
 * Version                : 1.0 
 * Date                   : 2008.2.20
 * Modification history   : 新建
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import com.sino.bean.Area;
import com.sino.bean.Basicinfo;
import com.sino.bean.Flzt;
import com.sino.bean.Hy;
import com.sino.bean.Qxkw;
import com.sino.bean.Sqqk;
import com.sino.bean.Xk;
import com.sino.bean.Xkqk;
import com.sino.bean.Xkzl;
import com.sino.bean.Zltype;
import com.sino.business.IBasicInfoService;
import com.sino.dao.IBasicInfoDAO;
import com.sino.model.BasicInfoModel;
import com.sino.util.DateUtils;

public class BasicInfoServiceImp implements IBasicInfoService {
	private IBasicInfoDAO basicInfoDAO = null;

	/**
	 * Description ：保存基本信息到数据库中
	 * 
	 * @param basicinfoModel
	 *            ：基本信息基础类
	 */
	public void saveBasicInfo(BasicInfoModel basicinfoModel) {
		Basicinfo basicinfo = this.getBasicInfo(basicinfoModel);
		if (this.validateExist(basicinfoModel.getZlid())) {
			this.getBasicInfoDAO().updateBasicInfo(basicinfo);
		} else {
			this.getBasicInfoDAO().saveBasicInfo(basicinfo);
		}
		basicinfoModel.setZlid(basicinfo.getZlid());
	}

	/**
	 * Description ：更新基本信息到数据库中
	 * 
	 * @param basicinfoModel
	 *            ：基本信息基础类
	 */
	public void updateBasicInfo(BasicInfoModel basicinfoModel) {
		Basicinfo basicinfo = this.getBasicInfoDAO().findBasicinfoById(
				basicinfoModel.getZlid());
		basicinfo.setIsqualified("1");

	}

	/**
	 * Description ：根据时间的专利号码修改密码
	 * 
	 * @param zlid
	 *            ：实际专利编号，
	 * @param passw
	 *            ：密码
	 */
	public void updatePassw(String zlid, String passw) {
		this.getBasicInfoDAO().updatePassw(zlid, passw);
	}

	/**
	 * Description ：根据专利编号查找基本信息
	 * 
	 * @param zlId
	 *            ：专利编号
	 */
	public BasicInfoModel findBasicinfoById(String zlId) {
		Basicinfo basicinfo = this.getBasicInfoDAO().findBasicinfoById(zlId);
		return this.getBasicInfoModel(basicinfo);
	}

	/**
	 * Description ：验证此专利编号在数据库中是否存在，返回true是存在，false不存在
	 * 
	 * @param zlId
	 *            ：专利编号
	 */
	public boolean validateExist(String zlId) {
		boolean retn = false;
		if (null != zlId && !"".equals(zlId) && zlId.length() > 0) {
			retn = this.getBasicInfoDAO().validateExist(zlId);

		}
		return retn;
	}

	/**
	 * Description ：根据专利编号查找基本信息,查找完毕后存在返回true，不存在返回false，然后请看缓存
	 * 
	 * @param zlId
	 *            ：专利编号==实际专利号码
	 */
	public boolean validateExistZlId(String zlId) {
		boolean retn = this.getBasicInfoDAO().validateExistZlId(zlId);
		return retn;
	}

	/**
	 * Description ：验证登陆信息,返回true验证通过，返回false验证失败
	 * 
	 * @param zlId
	 *            ：专利编号
	 */
	public String validateLogin(String zlId, String passw) {
		List temp = this.getBasicInfoDAO().findBasicinfoByZlId(zlId);
		Iterator it = temp.iterator();
		Basicinfo basicinfo = null;
		while (it.hasNext()) {
			basicinfo = (Basicinfo) it.next();
		}
		if (basicinfo == null) {
			return null;
		} else if (!passw.equals(basicinfo.getPassw())) {
			return null;
		} else if (null != basicinfo.getIsqualified()
				&& "1".equals(basicinfo.getIsqualified())) {
			return "1";
		} else {
			return basicinfo.getZlid();
		}
	}

	// ------------------------------------------------数据转换
	public Basicinfo getBasicInfo(BasicInfoModel basicinfoModel) {
		Basicinfo basicinfo = new Basicinfo();
		Xkqk xkqk1 = new Xkqk();
		Xkqk xkqk2 = new Xkqk();
		Xkqk xkqk3 = new Xkqk();
		Xkqk xkqk4 = new Xkqk();
		Xkqk xkqk5 = new Xkqk();
		Xkqk xkqk6 = new Xkqk();

		Sqqk sqqk1 = new Sqqk();
		Sqqk sqqk2 = new Sqqk();
		Sqqk sqqk3 = new Sqqk();
		Sqqk sqqk4 = new Sqqk();
		Sqqk sqqk5 = new Sqqk();
		Sqqk sqqk6 = new Sqqk();

		Area area1 = new Area();
		Area area2 = new Area();
		Area area3 = new Area();
		Area area4 = new Area();
		Area area5 = new Area();
		Area area6 = new Area();

		Flzt flzt1 = new Flzt();
		Flzt flzt2 = new Flzt();
		Flzt flzt3 = new Flzt();
		Flzt flzt4 = new Flzt();
		Flzt flzt5 = new Flzt();
		Flzt flzt6 = new Flzt();

		Xkzl xkzl1 = new Xkzl();
		Xkzl xkzl2 = new Xkzl();
		Xkzl xkzl3 = new Xkzl();
		Xkzl xkzl4 = new Xkzl();
		Xkzl xkzl5 = new Xkzl();
		Xkzl xkzl6 = new Xkzl();

		Hy hy = new Hy();
		hy.setId(basicinfoModel.getHy());

		Xk xk = new Xk();
		xk.setId(basicinfoModel.getXk());

		Zltype zltype = new Zltype();
		zltype.setId(basicinfoModel.getZltype());

		Qxkw qxkw = new Qxkw();
		qxkw.setId(basicinfoModel.getQxkw());
		// ---------------------------------------许可情况
		if (basicinfoModel.getXkqkId1() != null) {
			xkqk1.setId(basicinfoModel.getXkqkId1());
		}
		xkqk1.setHtlx(basicinfoModel.getHtlx1());
		xkqk1.setTc(basicinfoModel.getTc1());
		xkqk1.setXkdw(basicinfoModel.getXkdw1());
		xkqk1.setXkje(basicinfoModel.getXkje1());
		xkzl1.setId(basicinfoModel.getXktype1());
		xkqk1.setXktype(xkzl1);
		xkqk1.setBasicinfo(basicinfo);

		if (basicinfoModel.getXkqkId2() != null) {
			xkqk2.setId(basicinfoModel.getXkqkId2());
		}
		xkqk2.setTc(basicinfoModel.getTc2());
		xkqk2.setHtlx(basicinfoModel.getHtlx2());
		xkqk2.setXkdw(basicinfoModel.getXkdw2());
		xkqk2.setXkje(basicinfoModel.getXkje2());
		xkzl2.setId(basicinfoModel.getXktype2());
		xkqk2.setXktype(xkzl2);
		xkqk2.setBasicinfo(basicinfo);

		if (basicinfoModel.getXkqkId3() != null) {
			xkqk3.setId(basicinfoModel.getXkqkId3());
		}
		xkqk3.setTc(basicinfoModel.getTc3());
		xkqk3.setHtlx(basicinfoModel.getHtlx3());
		xkqk3.setXkdw(basicinfoModel.getXkdw3());
		xkqk3.setXkje(basicinfoModel.getXkje3());
		xkzl3.setId(basicinfoModel.getXktype3());
		xkqk3.setXktype(xkzl3);
		xkqk3.setBasicinfo(basicinfo);

		if (basicinfoModel.getXkqkId4() != null) {
			xkqk4.setId(basicinfoModel.getXkqkId4());
		}
		xkqk4.setTc(basicinfoModel.getTc4());
		xkqk4.setHtlx(basicinfoModel.getHtlx4());
		xkqk4.setXkdw(basicinfoModel.getXkdw4());
		xkqk4.setXkje(basicinfoModel.getXkje4());
		xkzl4.setId(basicinfoModel.getXktype4());
		xkqk4.setXktype(xkzl4);
		xkqk4.setBasicinfo(basicinfo);

		if (basicinfoModel.getXkqkId5() != null) {
			xkqk5.setId(basicinfoModel.getXkqkId5());
		}
		xkqk5.setTc(basicinfoModel.getTc5());
		xkqk5.setHtlx(basicinfoModel.getHtlx5());
		xkqk5.setXkdw(basicinfoModel.getXkdw5());
		xkqk5.setXkje(basicinfoModel.getXkje5());
		xkzl5.setId(basicinfoModel.getXktype5());
		xkqk5.setXktype(xkzl5);
		xkqk5.setBasicinfo(basicinfo);

		if (basicinfoModel.getXkqkId6() != null) {
			xkqk6.setId(basicinfoModel.getXkqkId6());
		}
		xkqk6.setTc(basicinfoModel.getTc6());
		xkqk6.setHtlx(basicinfoModel.getHtlx6());
		xkqk6.setXkdw(basicinfoModel.getXkdw6());
		xkqk6.setXkje(basicinfoModel.getXkje6());
		xkzl6.setId(basicinfoModel.getXktype6());
		xkqk6.setXktype(xkzl6);
		xkqk6.setBasicinfo(basicinfo);

		// ---------------------------------------申请情况
		if (basicinfoModel.getSqqkId1() != null) {
			sqqk1.setId(basicinfoModel.getSqqkId1());
		}
		area1.setId(basicinfoModel.getArea1());
		flzt1.setId(basicinfoModel.getFlzt1());
		sqqk1.setArea(area1);
		sqqk1.setFlzt(flzt1);
		sqqk1.setSqrq(DateUtils.formatDateTime(basicinfoModel.getSqrq1(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		sqqk1.setBasicinfo(basicinfo);

		if (basicinfoModel.getSqqkId2() != null) {
			sqqk2.setId(basicinfoModel.getSqqkId2());
		}
		area2.setId(basicinfoModel.getArea2());
		flzt2.setId(basicinfoModel.getFlzt2());
		sqqk2.setArea(area2);
		sqqk2.setFlzt(flzt2);
		sqqk2.setSqrq(DateUtils.formatDateTime(basicinfoModel.getSqrq2(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		sqqk2.setBasicinfo(basicinfo);

		if (basicinfoModel.getSqqkId3() != null) {
			sqqk3.setId(basicinfoModel.getSqqkId3());
		}
		area3.setId(basicinfoModel.getArea3());
		flzt3.setId(basicinfoModel.getFlzt3());
		sqqk3.setArea(area3);
		sqqk3.setFlzt(flzt3);
		sqqk3.setSqrq(DateUtils.formatDateTime(basicinfoModel.getSqrq3(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		sqqk3.setBasicinfo(basicinfo);

		if (basicinfoModel.getSqqkId4() != null) {
			sqqk4.setId(basicinfoModel.getSqqkId4());
		}
		area4.setId(basicinfoModel.getArea4());
		flzt4.setId(basicinfoModel.getFlzt4());
		sqqk4.setArea(area4);
		sqqk4.setFlzt(flzt4);
		sqqk4.setSqrq(DateUtils.formatDateTime(basicinfoModel.getSqrq4(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		sqqk4.setBasicinfo(basicinfo);

		if (basicinfoModel.getSqqkId5() != null) {
			sqqk5.setId(basicinfoModel.getSqqkId5());
		}
		area5.setId(basicinfoModel.getArea5());
		flzt5.setId(basicinfoModel.getFlzt5());
		sqqk5.setArea(area5);
		sqqk5.setFlzt(flzt5);
		sqqk5.setSqrq(DateUtils.formatDateTime(basicinfoModel.getSqrq5(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		sqqk5.setBasicinfo(basicinfo);

		if (basicinfoModel.getSqqkId6() != null) {
			sqqk6.setId(basicinfoModel.getSqqkId6());
		}
		area6.setId(basicinfoModel.getArea6());
		flzt6.setId(basicinfoModel.getFlzt6());
		sqqk6.setArea(area6);
		sqqk6.setFlzt(flzt6);
		sqqk6.setSqrq(DateUtils.formatDateTime(basicinfoModel.getSqrq6(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		sqqk6.setBasicinfo(basicinfo);

		Set sqqkSet = new HashSet();
		Set xkqkSet = new HashSet();
		sqqkSet.add(sqqk1);
		sqqkSet.add(sqqk2);
		sqqkSet.add(sqqk3);
		sqqkSet.add(sqqk4);
		sqqkSet.add(sqqk5);
		sqqkSet.add(sqqk6);

		xkqkSet.add(xkqk1);
		xkqkSet.add(xkqk2);
		xkqkSet.add(xkqk3);
		xkqkSet.add(xkqk4);
		xkqkSet.add(xkqk5);
		xkqkSet.add(xkqk6);
		basicinfo.setSqqks(sqqkSet);
		basicinfo.setXkqks(xkqkSet);
		basicinfo.setFmr(basicinfoModel.getFmr());
		basicinfo.setHy(hy);
		basicinfo.setLxdh(basicinfoModel.getLxdh());
		basicinfo.setLxr(basicinfoModel.getLxr());
		basicinfo.setName(basicinfoModel.getName());
		basicinfo.setPassw(basicinfoModel.getPassw());
		basicinfo.setShbz(basicinfoModel.getShbz());
		basicinfo.setShrq(basicinfoModel.getShrq());
		basicinfo.setSqrq(DateUtils.getSystemDate());
		basicinfo.setTxdz(basicinfoModel.getTxdz());
		basicinfo.setXk(xk);
		basicinfo.setYzbm(basicinfoModel.getYzbm());
		basicinfo.setYddh(basicinfoModel.getYddh());
		basicinfo.setZlid(basicinfoModel.getZlid());
		basicinfo.setZlid2(basicinfoModel.getZlid2());
		basicinfo.setZlqr(basicinfoModel.getZlqr());
		basicinfo.setZlrq(basicinfoModel.getZlrq());
		basicinfo.setSbr(basicinfoModel.getSbr());
		basicinfo.setSbrLxdh(basicinfoModel.getSbrLxdh());
		basicinfo.setZltype(zltype);
		basicinfo.setQxkw(qxkw);
		basicinfo.setShbz("0");
		basicinfo.setZlsqrq(basicinfoModel.getZlSqrq());
		basicinfo.setZxbz(basicinfoModel.getZxbz());
		basicinfo.setEmail(basicinfoModel.getEmail());
		basicinfo.setYear(basicinfoModel.getYear());
		if (null != basicinfoModel.getOtherXkName()) {
			basicinfo.setOtherXkName(basicinfoModel.getOtherXkName());
		}
		return basicinfo;
	}

	public BasicInfoModel getBasicInfoModel(Basicinfo basicinfo) {
		BasicInfoModel basicInfoModel = new BasicInfoModel();
		if (null != basicinfo.getHy() && null != basicinfo.getHy().getId()) {
			basicInfoModel.setHy(basicinfo.getHy().getId());
		}
		basicInfoModel.setLxdh(basicinfo.getLxdh());
		basicInfoModel.setFmr(basicinfo.getFmr());
		basicInfoModel.setLxr(basicinfo.getLxr());
		basicInfoModel.setName(basicinfo.getName());
		basicInfoModel.setPassw(basicinfo.getPassw());
		basicInfoModel.setPassw1(basicinfo.getPassw());
		if (null != basicinfo.getQxkw()) {
			basicInfoModel.setQxkw(basicinfo.getQxkw().getId());
		}
		basicInfoModel.setShbz(basicinfo.getShbz());
		basicInfoModel.setShrq(basicinfo.getShrq());
		basicInfoModel.setSqrq(DateUtils.formatDate(basicinfo.getSqrq(),
				DateUtils.FORMAT_DATE_YYYY_MM_DD));
		basicInfoModel.setTxdz(basicinfo.getTxdz());
		if (null != basicinfo.getXk()) {
			basicInfoModel.setXk(basicinfo.getXk().getId());
			basicInfoModel.setXkName(basicinfo.getXk().getName());
		}
		basicInfoModel.setYddh(basicinfo.getYddh());
		basicInfoModel.setYzbm(basicinfo.getYzbm());
		basicInfoModel.setZlid(basicinfo.getZlid());
		basicInfoModel.setZlid2(basicinfo.getZlid2());
		basicInfoModel.setZlqr(basicinfo.getZlqr());
		basicInfoModel.setZlrq(basicinfo.getZlrq());
		if (null != basicinfo.getZltype()) {
			basicInfoModel.setZltype(basicinfo.getZltype().getId());
		}
		basicInfoModel.setZltype1(basicInfoModel.getZltype().substring(0, 3));
		if (null != basicinfo.getZltype()) {
			basicInfoModel.setZltypemc(basicinfo.getZltype().getName());
		}
		basicInfoModel.setXkmc(basicinfo.getXk().getName());
		basicInfoModel.setHymc(basicinfo.getHy().getName());
		basicInfoModel.setQxkwmc(basicinfo.getQxkw().getName());
		basicInfoModel.setSbr(basicinfo.getSbr());
		basicInfoModel.setSbrLxdh(basicinfo.getSbrLxdh());
		basicInfoModel.setEmail(basicinfo.getEmail());
		basicInfoModel.setZlSqrq(basicinfo.getZlsqrq());
		if (null != basicinfo.getOtherXkName()) {
			basicInfoModel.setOtherXkName(basicinfo.getOtherXkName());
		}
		Iterator sqqkIt = basicinfo.getSqqks().iterator();
		Iterator xkqkIt = basicinfo.getXkqks().iterator();
		// ----------------------------------------------申请情况
		int i = 1;
		Sqqk sqqk;
		while (sqqkIt.hasNext()) {
			sqqk = (Sqqk) sqqkIt.next();
			try {
				BeanUtils.copyProperty(basicInfoModel, "sqqkId" + i, sqqk
						.getId());
				BeanUtils.copyProperty(basicInfoModel, "area" + i, sqqk
						.getArea().getId());
				BeanUtils.copyProperty(basicInfoModel, "areamc" + i, sqqk
						.getArea().getName());
				BeanUtils.copyProperty(basicInfoModel, "sqrq" + i, DateUtils
						.formatDateTime(sqqk.getSqrq(),
								DateUtils.FORMAT_DATE_YYYY_MM_DD));
				BeanUtils.copyProperty(basicInfoModel, "flzt" + i, sqqk
						.getFlzt().getId());
				BeanUtils.copyProperty(basicInfoModel, "flztmc" + i, sqqk
						.getFlzt().getName());
				i++;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		// -------------------------------------------许可状况
		i = 1;
		Xkqk xkqk;
		while (xkqkIt.hasNext()) {
			xkqk = (Xkqk) xkqkIt.next();
			try {
				BeanUtils.copyProperty(basicInfoModel, "xkqkId" + i, xkqk
						.getId());
				BeanUtils.copyProperty(basicInfoModel, "xkdw" + i, xkqk
						.getXkdw());
				BeanUtils.copyProperty(basicInfoModel, "xktype" + i, xkqk
						.getXktype().getId());
				BeanUtils.copyProperty(basicInfoModel, "xktypemc" + i, xkqk
						.getXktype().getName());
				BeanUtils.copyProperty(basicInfoModel, "xkje" + i, xkqk
						.getXkje());
				BeanUtils.copyProperty(basicInfoModel, "htlx" + i, xkqk
						.getHtlx());
				BeanUtils.copyProperty(basicInfoModel, "tc" + i, xkqk.getTc());
				if (xkqk.getHtlx() != null) {
					if (xkqk.getHtlx().equals("1")) {
						BeanUtils.copyProperty(basicInfoModel, "htlxmc" + i,
								"是");
					} else if (xkqk.getHtlx().equals("0")) {
						BeanUtils.copyProperty(basicInfoModel, "htlxmc" + i,
								"否");
					}
				}
				i++;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return basicInfoModel;
	}

	public void updateZxbz(BasicInfoModel basicinfoModel) {
		Basicinfo basicinfo = this.getBasicInfoDAO().findBasicinfoById(
				basicinfoModel.getZlid());
		basicinfo.setZxbz(basicinfoModel.getZxbz());
		this.getBasicInfoDAO().updateBasicInfo(basicinfo);

	}

	// ----------------------------------------------数据接口
	public IBasicInfoDAO getBasicInfoDAO() {
		return basicInfoDAO;
	}

	public void setBasicInfoDAO(IBasicInfoDAO basicInfoDAO) {
		this.basicInfoDAO = basicInfoDAO;
	}

	public void delBasicInfo(BasicInfoModel basicinfoModel, String zlid) {
		this.basicInfoDAO.delBasicInfo(zlid);

	}

	public Integer findFenShu1() {
		Integer i = this.basicInfoDAO.findFenShu1();
		return i;
	}

	// 显示起始日期和结束日期
	public String displayTime() {
		return this.basicInfoDAO.findShuJuKuTime();
	}

	public String displayTime2() {
		return this.basicInfoDAO.findShuJuKuTime2();
	}

	// xyw 查找数据库中的时间
	public String findShuJuKuTime() {
		// TODO Auto-generated method stub
		// 数据库时间
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		// 系统时间
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		String beginTime = this.basicInfoDAO.findShuJuKuTime();
		String endTime = this.basicInfoDAO.findShuJuKuTime2();

		if ((beginTime.equals("") || null == beginTime)
				&& (endTime.equals("") || null == endTime)) {
			return "1";
		} else {
			try {
				Date beginDate = s.parse(beginTime);
				Date endDate = s.parse(endTime);
				if (date.after(beginDate) && date.before(endDate)) {
					return "1";
				} else {
					return "0";
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}